package com.blue.service;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {

    @Pointcut("execution(* com.blue.service.UserService.insertUser(..))")
    public void commonPointcut() {
    }

    // 日志记录
    @Before("execution(* com.blue.service.UserService.*User*(..))")
    public void Log(JoinPoint joinPoint) {
        System.out.println("简单的日志记录...");

        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        String declaringTypeName = signature.getDeclaringTypeName();
        System.out.println("目标方法的方法名为: " + name);
        System.out.println("类的全限定类名: " + declaringTypeName);
    }

    // 前置通知
    @Before("commonPointcut()")
    public void beforeAdvice() {
        System.out.println("前置通知");
    }

    // 后置通知
    @AfterReturning("commonPointcut()")
    public void afterReturningAdvice() {
        System.out.println("后置通知");
    }

    // 环绕通知
    @Around("commonPointcut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("前置环绕通知");
        joinPoint.proceed();
        System.out.println("后置环绕通知");

        return null;
    }

    // 异常通知
    @AfterThrowing("commonPointcut()")
    public void afterThrowingAdvice() {
        System.out.println("异常通知");
    }

    // 最终通知
    @After("commonPointcut()")
    public void afterAdvice() {
        System.out.println("最终通知");
    }
}
